# TODO:
# - converter entidades html para acentos

from lib.BeautifulSoup import BeautifulSoup
from lib.utils import prettify
import re

#import codecs

result = open('candidate.html')
#result = open('candidate_error.html')
content = result.read()

soup = BeautifulSoup(content)

title = str(soup.html.head.title.contents[0])
print title
if re.search(u'erro ocorrido', title, re.IGNORECASE) != None:
    print 'log'

content_table = soup.html.body.form.findAll('table', recursive=False)[2].tr.findAll('td', recursive=False)[1].table
content_table_lines = content_table.findAll('tr', recursive=False)

content_table_1st_line = content_table_lines[0].findAll('td')
content_table_2nd_line = content_table_lines[1].findAll('td')
content_table_3rd_line = content_table_lines[2].findAll('td')
content_table_4th_line = content_table_lines[3].findAll('td')
content_table_5th_line = content_table_lines[4].findAll('td')
content_table_6th_line = content_table_lines[5].findAll('td')
content_table_7th_line = content_table_lines[7].findAll('td')
content_table_8th_line = content_table_lines[8].findAll('td')
content_table_9th_line = content_table_lines[9].findAll('td')
content_table_10th_line = content_table_lines[10].findAll('td')
content_table_11th_line = content_table_lines[11].findAll('td')
content_table_12th_line = content_table_lines[12].findAll('td')
content_table_13th_line = content_table_lines[13].findAll('td')
content_table_14th_line = content_table_lines[14].findAll('td')

goods_table_lines = soup.find('div', id='div_bens').table.findAll('tr', attrs = {'class': re.compile('(linhaPreenchida|linhaNaoPreenchida)')}, recursive=False)

candidate = {}

candidate['nome_urna'] = prettify(content_table_1st_line[1].contents[0])
candidate['numero'] = prettify(content_table_1st_line[3].contents[0])

candidate['nome_completo'] = prettify(content_table_2nd_line[1].contents[0])
candidate['sexo'] = prettify(content_table_2nd_line[3].contents[0])

candidate['data_nascimento'] = prettify(content_table_3rd_line[1].contents[0])
candidate['estado_civil'] = prettify(content_table_3rd_line[3].contents[0])

candidate['nacionalidade'] = prettify(content_table_4th_line[1].contents[0])
candidate['naturalidade'] = prettify(content_table_4th_line[3].contents[0])

candidate['instrucao'] = prettify(content_table_5th_line[1].contents[0])
candidate['ocupacao'] = prettify(content_table_5th_line[3].contents[0])

candidate['site'] = prettify(content_table_6th_line[1].findAll('a')[0].contents[0]) if len(content_table_6th_line[1].findAll('a')[0].contents) > 0 else ''

candidate['partido'] = prettify(content_table_7th_line[1].contents[0])

candidate['coligacao'] = prettify(content_table_8th_line[1].contents[0])

candidate['composicao_coligacao'] = prettify(content_table_9th_line[1].contents[0])

candidate['cargo_que_concorre'] = prettify(content_table_10th_line[1].contents[0])

candidate['processo_protocolo'] = prettify(content_table_11th_line[1].contents[0])

candidate['resultado_eleicao'] = prettify(content_table_12th_line[1].contents[0])

candidate['julgamento_contas'] = prettify(content_table_13th_line[1].contents[0])

candidate['cnpj_campanha'] = prettify(content_table_14th_line[1].contents[0])

candidate['bens'] = []

for line in goods_table_lines:
    desc = prettify(line.findAll('td')[1].contents[0])
    good_type = prettify(line.findAll('td')[2].contents[0])
    value = prettify(line.findAll('td')[3].contents[0])
  
    candidate['bens'].append({'desc': desc, 'type': good_type, 'value': value})
  
#print candidate['bens']

#print candidate['nome_urna']
#print candidate['numero']
#print candidate['nome_completo']
#print candidate['sexo']
#print candidate['data_nascimento']
#print candidate['estado_civil']
#print candidate['nacionalidade']
#print candidate['naturalidade']
#print candidate['instrucao']
#print candidate['ocupacao']
#print candidate['partido']
#print candidate['coligacao']
#print candidate['composicao_coligacao']
#print candidate['cargo_que_concorre']
#print candidate['processo_protocolo']
#print candidate['resultado_eleicao']
#print candidate['julgamento_contas']
#print candidate['cnpj_campanha']

#output = codecs.open('output.txt', 'w', 'utf-8')
#output.write(candidate['nome_completo'])
#output.close()